package com.xboss.tv.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.xboss.tv.R;
import com.xboss.tv.bean.NewsBean;

import java.util.List;

/**
 * Created by Administrator on 2017/7/14.
 */

public class MusicDataAdapter extends RecyclerView.Adapter<MusicDataAdapter.NewsHolder> {

    private List<NewsBean> mDatas;
    private LayoutInflater inflater;
    private Context mContext;

    public OnMenuitemClickListener mClickListener;

    public interface OnMenuitemClickListener {
        public void onItemClick(String url, int position);
        public void onBackClick(Activity activity);
    }

    public void setOnMenuitemClickListener(OnMenuitemClickListener onMenuitemClickListener) {
        mClickListener = onMenuitemClickListener;
    }

    public MusicDataAdapter(Context context, List<NewsBean> beans) {

        mContext = context;
        inflater = LayoutInflater.from(context);
        mDatas = beans;
    }

    @Override
    public NewsHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.item_music_data, parent, false);

        return new NewsHolder(view);
    }

    @Override
    public void onBindViewHolder(final NewsHolder holder, final int position) {

        final NewsBean bean = mDatas.get(position);
        String pic = bean.pic;
        if ("".equals(pic)) {
            holder.image.setImageResource(R.mipmap.ic_launcher);
        } else {
            Glide.with(mContext).load(pic).into(holder.image);
        }
        holder.title.setText(bean.src);

        holder.linear.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {

                    mClickListener.onItemClick(bean.url, position);
                    return false;
                }else if(keyCode == KeyEvent.KEYCODE_BACK){
                    mClickListener.onBackClick((Activity) mContext);
                    return false;
                }
                return false;
            }
        });
        holder.linear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mClickListener.onItemClick(bean.url, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (null != mDatas) {
            return mDatas.size();
        }
        return 0;
    }

    class NewsHolder extends RecyclerView.ViewHolder {

        ImageView image;
        TextView title;
        LinearLayout linear;

        public NewsHolder(View view) {
            super(view);
            image = (ImageView) view.findViewById(R.id.item_music_image_pic);
            title = (TextView) view.findViewById(R.id.item_music_text_name);
            linear = (LinearLayout) view.findViewById(R.id.item_music_linear);
        }
    }
}
